/**
 * Copyright (c) 2014.
 * Private license for beyondts.
 * Contact beyondts for using, beyondts999@163.com
 */
package net.beyondts.mplug.utils;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOCase;

import java.io.File;

/**
 * Project: beyondts-basic<br/>
 * Description: <br/>
 * Created at: 2014年8月17日<br/>
 * Owned by beyondts.
 * 
 * @author blazecrystal
 *
 * @version 1.0.0
 */
public class SimpleFilenameFilter implements java.io.FilenameFilter {
	/**
	 * 过滤器.
	 */
	private String filter;

	/**
	 * 是否大小写敏感.
	 */
	private boolean capSense;

	/**
	 * 
	 * 构造函数.
	 * 
	 * @param filter String 文件名所要包含的字符串
	 * @param capSense boolean 是否大小写敏感
	 */
	public SimpleFilenameFilter(String filter, boolean capSense) {
		this.capSense = capSense;
		if (this.capSense) {
			this.filter = filter;
		} else {
			this.filter = filter.toLowerCase();
		}
	}

	/**
	 * 过滤规则， 支持wildcard.
	 * 
	 * @param dir File 文件所在目录
	 * @param name String 当前文件名
	 * @return boolean 是否应该选择
	 */
	@Override
	public boolean accept(File dir, String name) {
		String temp = null;
		if (!this.capSense) {
			temp = name.toLowerCase();
		}
		IOCase iocase = IOCase.SYSTEM;
		if (this.capSense) {
			iocase = IOCase.SENSITIVE;
		} else {
			iocase = IOCase.INSENSITIVE;
		}
		return FilenameUtils.wildcardMatch(temp, this.filter, iocase);
	}

	/**
	 * 获得filter.
	 * 
	 * @return filter
	 */
	public String getFilter() {
		return filter;
	}

	/**
	 * 设置filter.
	 * 
	 * @param filter 要设置的 filter
	 */
	public void setFilter(String filter) {
		this.filter = filter;
	}

	/**
	 * 获得capSense.
	 * 
	 * @return capSense
	 */
	public boolean isCapSense() {
		return capSense;
	}

	/**
	 * 设置capSense.
	 * 
	 * @param capSense 要设置的 capSense
	 */
	public void setCapSense(boolean capSense) {
		this.capSense = capSense;
	}
}
